//
//  CZMainTabBar.swift
//  weibo
//
//  Created by Ken on 16/3/19.
//  Copyright © 2016年 Ken. All rights reserved.
//

import UIKit

let TabBarItemCount = 5


class CZMainTabBar: UITabBar {

    override func layoutSubviews() {
        super.layoutSubviews()
        
        print("CZMainTabBar: layoutSubviews")
        
        //计算
        let width = frame.width / CGFloat(TabBarItemCount)
        
        //计算最左边的这个 frame ,后面使用 CGRectOffset
        let originFrame = CGRect(x: 0,y: 0,width: width,height: frame.height)
        
        var index = 0
        for view in subviews {
            let cls = NSClassFromString("UITabBarButton")
            if view.isKindOfClass(cls!) {
                view.frame = CGRectOffset(originFrame, CGFloat(index)*width, 0)
                
                index += index == 1 ? 2 : 1
//                index++
//                if index == 2 {
//                    index++
//                }
            }
        }
    
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: frame.height)
        
        
    }

    //MARK: - 懒加载
    ///加号按钮
    private lazy var composeButton: UIButton = {
       let button = UIButton()
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //添加到 tabBar上面
        self.addSubview(button)
        
        return button
    }()
}

